<script>
function UpdateFormFlow(sURLAppend) {
	document.eForm.action=document.eForm.action + "&" + sURLAppend + "&edit=&SaveFF=&updateflow= "
	document.eForm.submit()
}
</script>
<CFIF IsDefined("url.SaveFF")>
	<CFINCLUDE TEMPLATE="../routing/_savedata.cfm">
</CFIF>
<cfset personID = "">
<cfif structkeyexists(url, "personnelidno")>
	<cfset personID = url.personnelidno>
<cfelse>
	<cfset personID = GetEntry.PERSONNELIDNO>
</cfif>

<cfset sPrevRcpt = personID>

<!--- no data-flow if eforms doesnt have processcode attached in it and eforms table doesnt have routeid --->
<CFSET lHasUserRole="No">

<cfquery name="createName" datasource="#client.company_dsn#">
	SELECT LASTNAME, FIRSTNAME, MIDDLENAME
	FROM CMFPA
	WHERE PERSONNELIDNO = '#sPrevRcpt#'
</cfquery>

<cfset cfMyName = createName.LASTNAME & ", " & createName.FIRSTNAME & " " & createName.MIDDLENAME>

<CFMODULE TEMPLATE="#client.common_path#cfm_wswindow.cfm" TITLE="Form-Flow Related Information" SHOW="True">

<CFQUERY NAME="GetActs" DATASOURCE="#client.company_dsn#">
	SELECT *
	FROM CINFFACTIVITY
	WHERE
		<CFIF ISDEFINED("FORM.EDTFORMFLOWDESC")>
			<CFSET CLIENT.newformflow="#FORM.EDTFORMFLOWDESC#">
			(PROCESSCODE = '#FORM.EDTFORMFLOWDESC#')
		<CFELSEIF ISDEFINED("CLIENT.newformflow")>
			(PROCESSCODE = '#CLIENT.newformflow#')
		<CFELSE>
			(PROCESSCODE = '#GetForm.PROCESSCODE#')
		</CFIF>
	ORDER BY ORDERID, DESCRIPTION
</CFQUERY>

<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
	<!--- extract initialization process --->
	<CFQUERY NAME="GetInit" DATASOURCE="#client.company_dsn#">
		SELECT D.*, M.ORIGINATOR
		FROM CRGROUTEDETAILS D, CRGROUTEMAIN M
		WHERE
			(M.ROUTEID = D.ROUTEID) AND
			(D.ROUTEID = '#GetEntry.ROUTEID#') AND
			(D.ORDERID = '1')
	</CFQUERY>
	<CFQUERY NAME="GetDiscard" DATASOURCE="#client.company_dsn#">
		SELECT D.*, M.ORIGINATOR
		FROM CRGROUTEDETAILS D, CRGROUTEMAIN M
		WHERE
			(M.ROUTEID = D.ROUTEID) AND
			(D.ROUTEID = '#GetEntry.ROUTEID#') AND
			D.STATUS ='DISREGARDED'
	</CFQUERY>
	<CFQUERY NAME="qryGetTime" DATASOURCE="#client.company_dsn#">
		SELECT TIMELASTUPDATE
		FROM CRGROUTEDETAILS
		WHERE (ROUTEID = '#GetEntry.ROUTEID#') AND (STATUS = 'STARTED')
	</CFQUERY>
	<TR>
		<TD COLSPAN=3 CLASS="bold">
			<!---<CFIF GetInit.RecordCount AND NOT ParameterExists(url.view)>  modified by jep 062508--->
			<CFIF GetInit.RecordCount>
				<CFOUTPUT>
				This form has been routed by&nbsp;<U>#GetInit.ORIGINATOR#</U>&nbsp;last&nbsp;
				<U>#DayOfWeekAsString(DayOfWeek(GetInit.DATELASTUPDATE))#,&nbsp;
				#DateFormat(GetInit.DATELASTUPDATE, "mmmm d, yyyy")#</U>&nbsp;at&nbsp;
				<!---<U>#TimeFormat(GetInit.DATELASTUPDATE, "hh:mmtt")#</U>.<BR>--->
				<U>#qryGetTime.TIMELASTUPDATE#</U>.<BR>
				<CFIF GetDiscard.RECORDCOUNT>
					This form has been disregarded by&nbsp;<U>#GetDiscard.ORIGINATOR#</U>&nbsp;last&nbsp;
					<U>#DayOfWeekAsString(DayOfWeek(GetDiscard.DATELASTUPDATE))#,&nbsp;
					#DateFormat(GetDiscard.DATELASTUPDATE, "mmmm d, yyyy")#</U>&nbsp;at&nbsp;
					<!---<U>#TimeFormat(GetInit.DATELASTUPDATE, "hh:mmtt")#</U>.<BR>--->
					<U>#GetDiscard.TIMELASTUPDATE#</U> 
					<CFIF GetDiscard.ROUTERREMARKS NEQ "" AND GetDiscard.ROUTERREMARKS NEQ "|NULL|">
						with the following remarks: "#GetDiscard.ROUTERREMARKS#"
					</CFIF>.<BR>
				</CFIF>
				</CFOUTPUT>
				<!--- Added by Mhel --->
				<!--- Start --->
				<CFSET lIsRouted="Yes">
				<!--- End --->
			<CFELSE>
				<BR>
				This form has NOT been routed.<BR>
				<!--- Added by Mhel --->
				<!--- Start --->
				<CFSET lIsRouted="No">
				<!--- End --->
			</CFIF>
			<CFIF #lcase(GetEntry.APPROVED)# EQ "r" OR #lcase(GetEntry.APPROVED)# EQ "d">
				<CFSET lIsRouted="No">
			</CFIF>
			<CFIF #lcase(GetEntry.APPROVED)# EQ "y">
				Form has been approved.
			</CFIF>
			<HR>
		</TD>
	</TR>
	<CFIF NOT GetActs.RecordCount>
		<TR>
			<TD COLSPAN=3 CLASS="bold11">
				<FONT COLOR="#FF0000">
				This form cannot be routed, the attached process is invalid.<br>
				Please contact your iBOS/e Administrator.
				</FONT>
			</TD>
		</TR>
		<CFSET lValidRoute  = False>
	</CFIF>
	<CFIF NOT isDefined("url.view") OR GetInit.RecordCount NEQ 0>
		<CFOUTPUT QUERY="GetActs">
		<TR VALIGN="top">
			<TD WIDTH="50%">
				<CFSETTING ENABLECFOUTPUTONLY="Yes">
	
				<!--- ************************************************************* --->
				<!--- ************************************************************* --->
				<!--- ******    THE ROUTING PATH IS DEFINED AND EXTRACTED   ******* --->
				<!--- ******    THE PROCESS STARTS HERE                     ******* --->
				<!--- ************************************************************* --->
				<!--- ************************************************************* --->
	
				<CFSET lUserRole="No">
				<CFSET sIntentedRec  = "">
				<CFSWITCH EXPRESSION="#lcase(GetActs.MAPPING)#">
	
					<!--------------------------------------->
					<!--- the originator is the recipient --->
					<!--------------------------------------->
					<CFCASE VALUE="originator">
						<CFIF GetActs.CurrentRow EQ 1>
							<CFSET sIntentedRec = "<FONT COLOR=RED>Invalid! The originator cannot be the recipient of the first activity.</FONT>">
							<CFSET lValidRoute  = False>
						<CFELSE>
							<CFSET sIntentedRec = "(" & sPrevRcpt & ") " & client.myname>
						</CFIF>
	
						<CFSET sPrevRcpt=personID>
					</CFCASE>
	
					<!-------------------------------------------------->
					<!--- extract the recipient through the unitcode --->
					<!-------------------------------------------------->
					<CFCASE VALUE="userrole">
						<!--- get unitcode to use --->
						<CFSET sUnitCode2Use=Trim(GetActs.UNITCODE)>
						<CFIF sUnitCode2Use IS "">
							<CFSET sUnitCode2Use="PERSONNELIDNO">
						</CFIF>
	
						<!--- get intended recipients --->
						<CFQUERY NAME="GetRecipients" DATASOURCE="#client.global_dsn#">
							SELECT USERGRPMEMBERSIDX, GUID FROM EGRGROLEINDEX, EGRGUSERMASTER
							WHERE
								EGRGUSERMASTER.USERID = EGRGROLEINDEX.USERGRPMEMBERSIDX AND
								USERGRPID_FK = '#GetActs.ROLECODE#';
						</CFQUERY>
	
						<!--- if sole individual in role, SIYA NA! --->
						<CFIF GetRecipients.RecordCount EQ 1>
							<CFMODULE TEMPLATE="#client.common_path#cfm_getuser.cfm" USERID="#GetRecipients.USERGRPMEMBERSIDX#">
							<CFIF ISFOUND>
								<CFSET sIntentedRec = sIntentedRec & "(" & PERID & ") " & PERNAME & "<BR>">
							</CFIF>
						<CFELSE>
							<!--- if two or more recipients, extract one with same unitcode as originator --->
							<CFSET sValidActivityID=Replace(GetActs.ACTIVITYID, "-", "", "ALL")>
							<CFSET lUserRole="Yes">
	
								<CFQUERY NAME="overridemapping" DATASOURCE="#client.company_dsn#">
									SELECT *
									FROM ECMFUSRDEFINEROLE
									WHERE (ROUTEID = '#GetEntry.ROUTEID#') AND
										  (ACTIVITYID = '#GetActs.ACTIVITYID#')
								</CFQUERY>
							<CFQUERY NAME="getdivision" DATASOURCE="#client.company_dsn#">
								SELECT #sUnitCode2Use#
								FROM CMFPA
								WHERE PERSONNELIDNO = '#sPrevRcpt#'
							</CFQUERY>
							<!--- by winston, pls chk, im nt sure... --->
							<!---added by jep--->
							<CFPARAM NAME="sCode2Use" DEFAULT="">
							<CFIF IsDefined("form.sel#sValidActivityID#")>
								<CFSET sCode2Use=Trim(Evaluate("form.sel#sValidActivityID#"))>
								<CFIF sCode2Use IS NOT "">
									<CFQUERY DATASOURCE="#client.global_dsn#" NAME="overridemapping">
										SELECT USERID
										FROM EGRGUSERMASTER
										WHERE GUID = '#sCode2Use#'
									</CFQUERY>
									<CFSET sCode2Use=overridemapping.USERID>
								</CFIF>
							<CFELSEIF lIsRouted>
								<CFSET sCode2Use=overridemapping.USERID>
							</CFIF>
							<CFIF sCode2Use EQ "">
								<CFSET sIntentedRec ="">
							<CFELSE>
								<CFMODULE TEMPLATE="#client.common_path#cfm_getuser.cfm" USERID="#sCode2Use#">
								<CFIF PERID NEQ "#sPrevRcpt#">
									<CFSET sIntentedRec = sIntentedRec & "(" & PERID & ") " & PERNAME & "<BR>">
									<CFSET sPrevRcpt=PERID>
								<CFELSE>
									<CFSET sIntentedRec ="<FONT COLOR=red>Select approver. Originator should not be Approver.</FONT>">
									<CFSET lValidRoute  = False>
								</CFIF>
							</CFIF>
							<!---added by jep ends here--->
							<!---
							<CFLOOP QUERY="GetRecipients">
								<CFSET sCode2Use="">
	
								<CFIF IsDefined("form.sel#sValidActivityID#")>
									<CFSET sCode2Use=Trim(Evaluate("form.sel#sValidActivityID#"))>
									<CFIF sCode2Use IS NOT "">
										<CFQUERY DATASOURCE="#client.global_dsn#" NAME="overridemapping">
											SELECT USERID
											FROM EGRGUSERMASTER
											WHERE GUID = '#sCode2Use#'
										</CFQUERY>
										<CFSET sCode2Use=overridemapping.USERID>
									</CFIF>
								<CFELSEIF lIsRouted>
									<CFSET sCode2Use=overridemapping.USERID>
								</CFIF>
	
								<CFIF sCode2Use IS "">
									<CFQUERY NAME="GetEntity" DATASOURCE="#client.company_dsn#">
										SELECT #sUnitCode2Use#
										FROM CMFPA
										WHERE GUID = '#GetRecipients.GUID#'
									</CFQUERY>
								</CFIF>
	
								<CFIF (
										(sCode2Use IS NOT "") AND
										(sCode2Use IS GetRecipients.USERGRPMEMBERSIDX)
									  ) OR
									  (
										(sCode2Use IS "") AND
										(GetEntity.RecordCount) AND
									    (GetDivision.RecordCount) AND
									    (
										    ucase(Evaluate("GetEntity." & sUnitCode2Use)) EQ
											ucase(Evaluate("getdivision." & sUnitCode2Use))
										)
									  )>
									<CFMODULE TEMPLATE="#client.common_path#cfm_getuser.cfm" USERID="#GetRecipients.USERGRPMEMBERSIDX#">
									<CFIF ISFOUND>
										<CFIF PERID NEQ "#CLIENT.CHAPA#">
											<CFSET sIntentedRec = sIntentedRec & "(" & PERID & ") " & PERNAME & "<BR>">
											<CFSET sPrevRcpt=PERID>
										<CFELSE>
											<CFSET sIntentedRec ="<FONT COLOR=red>Select approver. Originator should not be Approver.</FONT>">
											<CFSET lValidRoute  = False>
										</CFIF>
									</CFIF>
								</CFIF>
							</CFLOOP>
							--->
						</CFIF>
					</CFCASE>
	
					<!----------------------------------------------->
					<!--- the immediate superior is the recipient --->
					<!----------------------------------------------->
					<CFDEFAULTCASE> <!--- superior --->
						<!--- get superior code --->
						<CFQUERY NAME="GetCode" DATASOURCE="#client.company_dsn#">
							SELECT SUPERIORCODE, GUID, PERSONNELIDNO, LASTNAME #sqlCONCAT# ', ' #sqlCONCAT# FIRSTNAME #sqlCONCAT# ' ' #sqlCONCAT# MIDDLENAME AS NAME
							FROM CMFPA
							WHERE PERSONNELIDNO = '#sPrevRcpt#'
						</CFQUERY>
	
						<CFIF GetCode.RecordCount>
							<CFIF Trim(GetCode.SUPERIORCODE) NEQ "">
								<CFQUERY NAME="GetCode" DATASOURCE="#client.company_dsn#">
									SELECT GUID, PERSONNELIDNO, LASTNAME #sqlCONCAT# ', ' #sqlCONCAT# FIRSTNAME #sqlCONCAT# ' ' #sqlCONCAT# MIDDLENAME AS NAME
									FROM CMFPA
									WHERE PERSONNELIDNO = '#GetCode.SUPERIORCODE#'
								</CFQUERY>
							</CFIF>
	
							<!--- is superior a valid user --->
							<CFIF GetCode.RecordCount>
								<CFQUERY NAME="IsExists" DATASOURCE="#client.global_dsn#">
									SELECT USERID
									FROM EGRGUSERMASTER
									WHERE
										GUID = '#GetCode.GUID#'
								</CFQUERY>
	
								<CFIF IsExists.RecordCount>
									<CFSET sIntentedRec = "(" & GetCode.PERSONNELIDNO & ") " & GetCode.NAME>
									<CFSET sPrevRcpt=GetCode.PERSONNELIDNO>
								<CFELSE>
									<CFSET sIntentedRec = "<FONT COLOR=RED>Invalid! The recipient is not an iBOS/e Users.</FONT>">
									<CFSET lValidRoute  = False>
								</CFIF>
							</CFIF>
						</CFIF>
					</CFDEFAULTCASE>
				</CFSWITCH>
	
				<!--- ************************************************************* --->
				<!--- ************************************************************* --->
				<!--- ******    THE ROUTING PATH IS DEFINED AND EXTRACTED   ******* --->
				<!--- ******    THE PROCESS ENDS HERE                       ******* --->
				<!--- ************************************************************* --->
				<!--- ************************************************************* --->
	
				<!--- extract logs --->
				<!--- and action term made to be realistic... - winston--->
				<CFQUERY NAME="GetLog" DATASOURCE="#client.company_dsn#">
					SELECT A.*, C.STATUSDESC AS ACTIONMADE
					FROM CRGROUTEDETAILS A, CINFFACTIVITY B, CLKACTIONDETAILS C
					WHERE
						(A.STATUS = C.PROCESSINGACTION) AND
						(C.ACTIONCODE = B.ACTIONCODE) AND
						(A.ACTIVITYID = B.ACTIVITYID) AND
						(A.ACTIVITYID  = '#ACTIVITYID#') AND
						(A.ROUTEID     = '#GetEntry.ROUTEID#')
					ORDER BY A.CURRENTSEQUENCE, A.ROUTER, A.STATUS
				</CFQUERY>
	
				<CFSETTING ENABLECFOUTPUTONLY="No">
	
				<!--- scheduled activities --->
				<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH="100%">
					<TR>
						<TD NOWRAP>Sequence No.</TD>
						<TD CLASS="bold" WIDTH="100%">#ORDERID#</TD>
					</TR>
					<TR>
						<TD>Description&nbsp;</TD>
						<TD CLASS="bold">#DESCRIPTION#</TD>
					</TR>
					<TR VALIGN="top">
						<TD>Recipient/s</TD>
						<TD CLASS="bold">
							<CFIF sIntentedRec EQ "">
								<FONT COLOR="red">No intended recipient(s).</FONT>
								<CFSET lValidRoute  = False>
							<CFELSE>
								#sIntentedRec#
							</CFIF>
						</TD>
					</TR>
					<CFIF #len(trim(ACTIONCODE))# NEQ 0>
						<CFQUERY NAME="GetAction" DATASOURCE="#client.company_dsn#">
							SELECT DESCRIPTION
							FROM CLKACTIONS
							WHERE
								(ACTIONCODE = '#ACTIONCODE#')
						</CFQUERY>
					</CFIF>
					<!--- Added by Mhel --->
					<!--- Start --->
					<CFIF lUserRole AND NOT lIsRouted AND NOT ParameterExists(url.view)>
						<CFSET lHasUserRole="Yes">
						<CFSET lstGUID=quotedValuelist(GetRecipients.GUID, ',')>
						<CFIF lstGUID EQ "">
							<CFSET lstGUID="''">
						</CFIF>
						<CFQUERY NAME="getdivision" DATASOURCE="#client.company_dsn#">
							SELECT LASTNAME, FIRSTNAME, MIDDLENAME, GUID
							FROM CMFPA
							WHERE GUID IN (#preserveSingleQuotes(lstGUID)#)
							ORDER BY LASTNAME,FIRSTNAME,MIDDLENAME
						</CFQUERY>
	
						<tr>
							<td nowrap>Override Default Recipient:</td>
							<td><select name="sel#sValidActivityID#">
									<option value=" ">--Select new recipient--
									<CFLOOP QUERY="getdivision">
										<option value="#getdivision.GUID#">#getdivision.LASTNAME#, #getdivision.FIRSTNAME# #getdivision.MIDDLENAME#
									</CFLOOP>
								</select>
								<input type="hidden" name="hidden#sValidActivityID#" value="#sCode2Use#">
							</td>
						</tr>
					</CFIF>
					<!--- End --->
				</TABLE>
			</TD>
			<TD BGCOLOR="##808080"></TD>
			<TD WIDTH="50%">
			<CFLOOP QUERY="GetLog">
				<CFSET nAction = GetLog.CURRENTSEQUENCE - 1>
				<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH="100%">
					<TR><TD></TD><TD WIDTH="100%"></TD></TR>
					<TR>
						<TD>Status:</TD>
						<TD CLASS="bold">#GetLog.ACTIONMADE#</TD>
						<TD WIDTH="15" NOWRAP>Process No.:</TD>
						<TD CLASS="bold" WIDTH="10">#nAction#</TD>
					</TR>
					<TR>
						<TD VALIGN="top">Comments:</TD>
						<TD CLASS="bold" COLSPAN="3">#GetLog.ROUTERREMARKS#</TD>
					</TR>
					<TR>
						<TD>Source:</TD>
						<TD CLASS="bold" COLSPAN="3">#GetLog.ROUTER#</TD>
					</TR>
					<TR>
						<TD>Date/Time:</TD>
						<TD CLASS="bold" COLSPAN="3">
								#DayOfWeekAsString(DayOfWeek(GetLog.DATELASTUPDATE))#,&nbsp;
								#DateFormat(GetLog.DATELASTUPDATE, "mmmm d, yyyy")#&nbsp;
								#TimeFormat(GetLog.TIMELASTUPDATE, "hh:mmtt")#
						</TD>
					</TR>
					<CFIF FileExists(expandpath(client.image_path) & "signatures/" & GetLog.ROUTER & ".jpg")>
						<TR>
							<TD>Signature:</TD>
							<TD>
								<img src="#client.image_path#signatures/#GetLog.ROUTER#.jpg">
							</TD>
						</TR>
					</CFIF>
				</TABLE>
			</CFLOOP>
			</TD>
		</TR>
		<CFIF CurrentRow NEQ RecordCount>
			<TR><TD COLSPAN=3><HR></TD></TR>
		</CFIF>
		</CFOUTPUT>
	
		<!--- Added by Mhel --->
		<!--- Start --->
		<CFIF lHasUserRole>
			<CFSET sPKFields="">
			<CFIF Trim(GetForm.PK_FIELD1) IS NOT "">
				<CFIF ISDEFINED("URL.KEY1")>
					<CFSET sPKFields=GetForm.PK_FIELD1 & "=" & Evaluate("url.key1")>
	
					<CFIF Trim(GetForm.PK_FIELD2) IS NOT "">
						<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD2 & "=" & Evaluate("url.key2")>
	
						<CFIF Trim(GetForm.PK_FIELD3) IS NOT "">
							<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD3 & "=" & Evaluate("url.key3")>
	
							<CFIF Trim(GetForm.PK_FIELD4) IS NOT "">
								<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD4 & "=" & Evaluate("url.key4")>
	
								<CFIF Trim(GetForm.PK_FIELD5) IS NOT "">
									<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD5 & "=" & Evaluate("url.key5")>
								</CFIF>
							</CFIF>
						</CFIF>
					</CFIF>
				<CFELSE>
					<CFSET sPKFields=GetForm.PK_FIELD1 & "=" & Evaluate("url.#GetForm.PK_FIELD1#")>
	
					<CFIF Trim(GetForm.PK_FIELD2) IS NOT "">
						<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD2 & "=" & Evaluate("url.#GetForm.PK_FIELD2#")>
	
						<CFIF Trim(GetForm.PK_FIELD3) IS NOT "">
							<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD3 & "=" & Evaluate("url.#GetForm.PK_FIELD3#")>
	
							<CFIF Trim(GetForm.PK_FIELD4) IS NOT "">
								<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD4 & "=" & Evaluate("url.#GetForm.PK_FIELD4#")>
	
								<CFIF Trim(GetForm.PK_FIELD5) IS NOT "">
									<CFSET sPKFields=sPKFields & "&" & GetForm.PK_FIELD5 & "=" & Evaluate("url.#GetForm.PK_FIELD5#")>
								</CFIF>
							</CFIF>
						</CFIF>
					</CFIF>
				</CFIF>
			</CFIF>
			<CFOUTPUT>
				<CFIF NOT ISDEFINED("URL.KEY1")>
					<tr><td colspan=3><HR></td></tr>
					<tr><td><input type="button" value="Update Form-Flow" onclick="UpdateFormFlow('#sPKFields#')"></td><td colspan=2></td></tr>
				</CFIF>
			</CFOUTPUT>
		</CFIF>
		<!--- End --->
	</CFIF>
</TABLE>
<CFMODULE TEMPLATE="#client.common_path#cfm_wswindow.cfm" TOP="False">
